package fileIO;

import java.io.File;
import java.io.FileInputStream;

import javax.swing.JOptionPane;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;

import GUIMain.FileChooser;

public class XLSParser {
	public static HSSFWorkbook openXLS(String fileName){
		try{
			return new HSSFWorkbook(new FileInputStream(fileName));
		} catch (Exception e) {
			int choice = JOptionPane.showConfirmDialog(null, "Specified XLS is not found or cannot be opened.  Try opening the file and resaving as .XLS format.  Select another file?", "File Not Found", JOptionPane.OK_CANCEL_OPTION); 
			if(choice == JOptionPane.YES_OPTION){
				//Show File Input dialog and rerun class
				FileChooser.openFileChooser(null, false);
				File file = FileChooser.getSelectedFile();
				if(file==null){
					return null;
				}
				return openXLS(file.getAbsolutePath());
			}else{
				return null;
			}
		}
		
	}
	public static HSSFWorkbook openXLS(String fileName,boolean supressWarnings)
	throws Exception{
		try{
			return new HSSFWorkbook(new FileInputStream(fileName));
		} catch (Exception e) {
			if(!supressWarnings){
				int choice = JOptionPane.showConfirmDialog(null, "Specified XLS is not found or cannot be opened, select another file?", "File Not Found", JOptionPane.OK_CANCEL_OPTION); 
				if(choice == JOptionPane.YES_OPTION){
					//Show File Input dialog and rerun class
					FileChooser.openFileChooser(null, false);
					File file = FileChooser.getSelectedFile();
					if(file==null){
						return null;
					}
					return openXLS(file.getAbsolutePath());
				}else{
					return null;
				}
			}else{
				String message = "Input XLS Could not be opened - FilePath " + fileName;
				throw new Exception(message);
			}
		}
		
		
	}
}
